<?php
# $Id$
require_once(APP_LIB_DIR . 'Sajax.php');
require_once(APP_LIB_DIR . 'DBAccessModel.class.php');
require_once('utils.inc.php');

function getlist($p='',$search='',$by='1',$s='DESC') { 
    $a = new MessageTrackingManagerAction();
    parse_str($search,$res);
    return $a->getlist($p,$res,$by,$s); 
}
function lookup($what=''){
    $l = new MessageTrackingManagerAction();
    return $l->lookup($what);
}
function save($detail='',$id='',$id1='',$action=''){
    $s = new MessageTrackingManagerAction();
    parse_str($detail,$res);
    return $s->save($res,$id,$id1,$action);
}

sajax_init();
//$sajax_debug_mode = 1;
sajax_export('getlist');
sajax_export('lookup');
sajax_export('save');
sajax_handle_client_request();
$sajax_request_type = 'POST';
require_once('header.php'); 
$mm = new MessagingModel('Messaging');
$smsc= $mm->smsc();
?>
<script language="JavaScript" type="text/javascript">

<?php sajax_show_javascript();?>

var s = "ASC";
var b = 1;

function page(p) {
    st = $('form1').toQueryString();
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function sort(by){
    s = (s=="DESC") ? "ASC" : "DESC";
    st = $('form1').toQueryString();
    p = $('pagenum');
    p = (p) ? p.value : 1;
    b = by;
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function filter(){
    st = $('form1').toQueryString();
    p = 1
    //st = Json.toString(st);
    load();
    x_getlist(p,st,b,s,getlist_cb);
}
function getlist_cb (new_data){
    //alert(new_data)
    data = Json_eval(new_data);
    var fields = [ ["id","ID"],
                   ["message","Message"],
                   ["contacts","No. of Contacts"],
                   ["success","Successfull"],
                   ["pending","Pending"],
                   ["failure","Failure"],
                   ["reject","Rejected"],
                   ["buffered","Buffered"]
                 ]

    var  extra = [ [true,'id','edit'],
                   [true,'id','del']
                 ]
                 
    $("thetable").innerHTML = data[1]
    $("thetable").innerHTML += mytable1(data[0],fields, extra)
}

/*function levels_cb (new_data){
    data = Json_eval(new_data);
    AddToOptionList(data, $('level'), 'id','name')
}*/
function clearOrder(a){
  
         data = $('form2').toQueryString();
         $('myid').value=''
//alert("clearing list of cred");

}
function release_items() {
    data = $('form1').toQueryString();
    if ($('folders').value==""){
    $('release_button').disabled=true
          return false;
    }else{
    $('release_button').disabled=false
    if (!confirm("Are you sure to Re-send selected Messages?  \r\n \r\n")) {
        return;
    }
    $('release_button').disabled=true
    var folder_id = $('folders').value
    data = $('form2').toQueryString();
    $$('input').filterByAttribute('type','=','checkbox').filterByAttribute('class','=','groupitems').each(function(el,x){
        if (el.checked) {
				//if (qty_released[x].value.toInt() < qty_r[x].value.toInt()){
				//	alert("Use Unclaim button")
				//}else{
//alert(folder_id);
//alert(el.value);
			  		x_save('',el.value,folder_id,'move', function(new_data1){ });
				//}
			}else{
		//		alert("Not allowed")
			}
    });
	//alert($('myid').value)
         alert("Message Successfully Re-send.");
	edit($('myid').value)
    $('release_button').disabled=false
      filter();
     // show_t()
    
}
}
function check_folder(){
    data = $('form1').toQueryString();
    if ($('folders').value==""){
    $('release_button').disabled=true
          return false;
    }else{
    $('release_button').disabled=false
    }

}
function toggle_check(){
    data = $('form1').toQueryString();
    if ($('folders').value==""){
    $('release_button').disabled=true
    }else{
    $('release_button').disabled=false
    }
    $$('input').filterByAttribute('type','=','checkbox').filterByAttribute('class','=','groupitems').each(function(el,x){
        if (!el.disabled)
        el.checked=!el.checked
    });
}
function save () {
    data = $('form2').toQueryString();
    id = $('myid').value
    $('myid').value = ''
    x_save(data,id, function(new_data1){
      Json_eval(new_data1);
      filter();
      show_t()
    });
}

function sendmsg(){
    data = $('form2').toQueryString();
to = $('to').value
msg = $('message').value
checkmobile(to);
    x_save(data,'','','send_msg', function(new_data1){
      Json_eval(new_data1);
      filter();
      show_t()
    });
//alert(to);
//alert(msg);
}
function folders_cb (new_data){
         data = Json_eval(new_data);
         AddToOptionList(data, $('folders'), 'id','name')
}
/*function contactlist_cb (new_data){
         data = Json_eval(new_data);
         AddToOptionList(data, $('contacts'), 'id','name')
}*/
function show_t(){
    if($('thetable').style.display == "none"){
        hide_t('newform','thetable')
       // $('altfrm').value = "New Contacts"
    } else {
        hide_t('thetable','newform')
//        $('altfrm').value = "Back to List"
    }
    $('form2').reset();
    $('myid').value = ''
}

function edit (id) {
    if (confirm('Are you sure you want to resend this Message?')){
      x_save('',id,'','resend_msg', function(new_data1){ 
alert("Message id: " + id );

      filter();
      });
    }
}

function del (id) {
    if (confirm('Are you sure you want to delete this Record?')){
      x_save('',id,'','del', function(new_data1){
        Json_eval(new_data1);
        filter();
      })   
    }
}

function load (){
    //$("thetable").empty().addClass('ajax-loading');
    $("thetable").innerHTML = loading;
}

function update() {
       var old = document.Form1.counter.value;
       document.Form1.counter.value=document.Form1.message.value.length;
       if(document.Form1.counter.value > 160 && old <= 160) {
         alert('Too much data in the text box!');
         if(document.styleSheets) {
           document.Form1.counter.style.fontWeight = 'bold';
           document.Form1.counter.style.color = '#ff0000'; } }
       else if(document.Form1.counter.value <= limit && old > limit
	       && document.styleSheets ) {
           document.Form1.counter.style.fontWeight = 'normal';
           document.Form1.counter.style.color = '#000000'; } 
       }

    function textCounter(field, countfield, maxlimit) {
        if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters left' counter
        else
        countfield.value = maxlimit - field.value.length;
    }

    function CheckKey(e)
    {
        var unicode = e.keyCode? e.keyCode: e.charCode
        if (unicode == 60)
        {
            e.preventDefault();
        }
    }
    function CheckCtrl(e,r)
    {
        var unicode = e.keyCode? e.keyCode: e.charCode
        if (unicode == 17)
        {
           document.Form1.send_Message.focus();
           //return false;
            //r.value = ""//alert("Can't Paste")//e.preventDefault();
        }
    }
    function checkmobile(msisdn)
    {
      var re = new RegExp('^(09\\d{9}|222)$')
      if (msisdn.match(re) ) {
          return true;
      }else{
          alert('Incorrect Number')
          return false;
      }
    }  
function newPopup(url) {
	popupWindow = window.open(
		url,'popUpWindow','height=700,width=800,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
function goexport(){
    st = $('form1').toQueryString();
    URL = "?module=<?=DEFAULT_MODULE?>&action=MessageTrackingManager&export=1&" + st;
    window.location = URL
}
</script>
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/structure2.css" type="text/css" />
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/form2.css" type="text/css" />
<table width="100%" class="menubar" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td class="menudottedline" width="40%">
            <div class="pathway"><font size="2px" color="#000033"><b><?=$config['version']?></b></font></div>       
        </td>
        <td class="menudottedline" align="right">
            <table cellpadding="0" cellspacing="0" border="0" id="toolbar">
                <tr height="60" valign="middle" align="center">
                    <td>
                        <a class="toolbar" href="JavaScript:newPopup('?module=<?=DEFAULT_MODULE?>&action=FolderManager');">
                        <img src="<?=WEB_MODULE_DIR?>images/help_f2.png" 
                             align="middle" name="new" border="0"><br>Folders
                        </a>
                    </td>
                    <td>
                        <a class="toolbar" href="#">
                        <img src="<?=WEB_MODULE_DIR?>images/help_f2.png" 
                             align="middle" name="new" border="0"><br>Help
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<div align="center" class="centermain">
  <div class="main">
    <table class="adminheading" width="100%">
      <tr>
        <th class="cpanel" rowspan="2" nowrap>Message Tracking Management&nbsp;<font size="1px" color="#CCCCCC">[<?=strtoupper($auth['privilege'])?>]</font></th>
      </tr>
    </table>
  </div>
    <table class="adminlist" width="100%">
      <tr>
        <th class="title"><a href="javascript: show_tool('panel');">:: SEARCH PANEL</a></th>
      </tr>
      <tr>
        <td width="40%" valign="top">
        <!-- filter panel -->
          <div id="panel">
            <table class="adminlist" width="100%">
              <tr>
                <td>
                  <form id="form1" class="wufo" action="" method="post" onSubmit="filter();return false;">
                    <ul>
                      <li>
                            <span>
                            <label class="desc">Search Mobile:</label>
                            </span>
                        <span>
                          <select class="field select" style="width:10em" name="search[searchfield]">
                           <!--<option value="mem.fullname">FROM</option>-->
                            <option value="msg.message">Message</option>
                          </select>
                        </span>
                        
                        <span>
                          <input class="field text" size="15" name="search[searchtext]" value="" onKeyup="filter()">
                        </span>
                        <span>
                          <input id="saveForm" class="btTxt" type="button" value="Search" onClick="filter()"/>
                          <input class="btTxt" type="button" value="Clear" onClick="this.form.reset();filter()"/>
                          <input class="btTxt" type="button" value="Export" onClick="goexport()"/>
                          <input id="toggle_button" class="btTxt" type="button" value="Toggle Check" onClick="toggle_check()"/>
             <!--             <input id="altfrm" class="btTxt" type="button" value="New Student" onClick="show_t()"/>-->
                        </span>
                            <span>
                            <label class="desc">Move to Folder:</label>
                            </span>
                        <span>
                            <select class="field select" style="width:10em" name="search[folders]" id="folders" onChange="check_folder()">
                            <option value=""></option>
                            </select>
                        </span>
                        <span>
                            <input id="release_button" class="btTxt" type="button" value="Move" onClick="release_items()"/>
                        </span>  
                      </li>
                    </ul>
                  </form>
                </td>
              </tr>
            </table>
          </div>
        <!-- filter panel end -->
        </td>
      </tr>
    </table>
      <form id="form2" method="post" onSubmit="return false;">
        <table class="adminform" width="100%">
          <tr>
            <th class="title">:: SEARCH RESULTS</th>
          </tr>
          <tr>
            <td>
              <div id="thetable"><img src="/mcs/modpub/images/loading_small.gif"/></div>
            </td>
          </tr>
          <tr>
            <td align="center">
              <div id="newform" style="display: none" class="container">
                <input type="hidden" value="" id="myid" />
                <table class="adminform" align="center" width="100%">
                  <tr>
                    <th colspan="3">REPLY DATA</th>
                  </tr>
                  <tr>
                    <td><b>TO:</b></td> 
                  </tr>
                  <tr>
                    <td>       
            <!--     <select class="field select" style="width:4" id="contacts" onclick="get_number()">
                     </select>-->
                      <input class="field text" id="to" size="20" name="details[to]" maxlength="255" value="">
                    </td>
                  </tr>
                  <tr>
                    <td><b>SMSC:</b></td> 
                  </tr>
                  <tr>
                    <td>
                      <div>
		        <?=show_select3('network','network', 'details[smsc]',$smsc);?>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td><b>MESSAGE :</b></td>
                  </tr>
                  <tr>
                    <td align="left" style="width: 52%;">
                      <textarea name="details[message]" rows="2" cols="20" id="message" onKeyUp="textCounter(this.form.message,this.form.counter,160);" onchange="textCounter(this.form.message,this.form.counter,160);" onKeyPress="CheckKey(event)" onKeyDown="CheckCtrl(event,this)" style="height:90px;width:300px;"></textarea>
                      <div  style="height:10px;"></div>
                      <div>
                        <label style="font: Verdana, Arial, Helvetica, sans-serif; color: #6d6b6c">Message Count</label>&nbsp;&nbsp;
                        <input name="counter" type="text" value="160" id="counter" disabled="disabled" style="width:60px;" />
                      </div>
                    </td>
                  </tr>
                </table>
                <table class="adminform" align="center" width="100%">
                  <tr>
                    <td>
                      <input id="saveForm" class="btTxt" type="button" value="Send" onClick="sendmsg();"/>
                      <input id="saveForm" class="btTxt" type="button" value="Clear" onClick="this.form.reset();clearOrder();"/>
    <!--               <input id="saveForm" class="btTxt" value="Clear" onClick="clearOrders();"/>-->
                      <input id="saveForm" class="btTxt" type="button" value="Cancel" onClick="this.form.reset();clearOrder();hide_t('newform', 'thetable')"/>
                    </td>
                  </tr>
                </table>
              </div>
            </td>
          </tr>
        </table>
      </form>
<script type="text/javascript">
x_getlist(getlist_cb)
x_lookup("folders",folders_cb)
/*x_lookup("contacts",contactlist_cb)*/
</script>
<?php require_once('footer.php'); ?>
